package com.lill.product.service.impl;

import java.beans.PropertyDescriptor;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lill.product.dao.IBaseDiscountDao;
import com.lill.product.dao.IBaseRoyaltyDao;
import com.lill.product.model.BaseDiscountModel;
import com.lill.product.model.BaseRoyaltyModel;

@Service("baseProfitItem")
public class BaseProfitItemServiceImpl {
	
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private IBaseDiscountDao discountDao;
	@Autowired
	private IBaseRoyaltyDao royaltyDao;
	
	public String hasDiscountItems() {
		String result="";
		
		Object userObj=SecurityUtils.getSubject().getPrincipal();
//		String userId=null;
		String companyId=null;
		Object branchId=null;
		try {
//			userId = new PropertyDescriptor("id", userObj.getClass()).getReadMethod().invoke(userObj).toString();
			companyId = new PropertyDescriptor("companyId", userObj.getClass()).getReadMethod().invoke(userObj).toString();
			branchId = new PropertyDescriptor("branchId", userObj.getClass()).getReadMethod().invoke(userObj);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		if(StringUtils.isNotEmpty(companyId)){
			logger.info("companyId:"+companyId+"####branchId:"+branchId);
			if(StringUtils.isNotEmpty(companyId)){
				logger.info("companyId:"+companyId+"####branchId:"+branchId);
				Map<String, Object> mapParam=new HashMap<String, Object>();
				mapParam.put("companyId", companyId);
				if(null!=branchId){
					mapParam.put("branchId", branchId);
				}else{
					mapParam.put("branchIdIsNull", companyId);
				}
				
				List<BaseDiscountModel> discountList=discountDao.queryList(mapParam);
				StringBuffer formContent=new StringBuffer("");
				if(CollectionUtils.isNotEmpty(discountList)){
					for (BaseDiscountModel baseDiscount : discountList) {
						formContent.append("<Form-item label=\"").append(baseDiscount.getName())
							.append("\" prop=\"").append(baseDiscount.getMapName()).append("\">")
							.append("<i-input v-model=\"productDiscount.").append(baseDiscount.getMapName())
							.append("\" placeholder=\"").append(baseDiscount.getContent()+"\"/>")
							.append("</Form-item>");
					}
					formContent.append("<Form-item>");
					formContent.append("<i-button type=\"primary\" @click=\"handleDiscountSubmit('formDiscountValidate')\">提交</i-button>");
					formContent.append("<i-button type=\"warning\" @click=\"reload\" style=\"margin-left: 8px\"/>返回</i-button>");
					formContent.append("</Form-item>");
					result=formContent.toString();
				}
			}
		}
		return result;
	}
	
	/**
	 * @param mode 1代表销售台账   0 代表初始化设置
	 * @return
	 */
	public String hasRoyaltyItems(int mode) {
		String result="";
		
		Object userObj=SecurityUtils.getSubject().getPrincipal();
		String companyId=null;
		Object branchId=null;
		try {
			companyId = new PropertyDescriptor("companyId", userObj.getClass()).getReadMethod().invoke(userObj).toString();
			branchId = new PropertyDescriptor("branchId", userObj.getClass()).getReadMethod().invoke(userObj);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		if(StringUtils.isNotEmpty(companyId)){
			logger.info("companyId:"+companyId+"####branchId:"+branchId);
			Map<String, Object> mapParam=new HashMap<String, Object>();
			mapParam.put("companyId", companyId);
			if(null!=branchId){
				mapParam.put("branchId", branchId);
			}else{
				mapParam.put("branchIdIsNull", companyId);
			}
			StringBuffer formContent=new StringBuffer("");
//			if(1==mode){
//				formContent.append("<Form-item label=\"").append("按揭费用")
//				.append("\" prop=\"").append("mortProfit").append("\">")
//				.append( "<i-input v-model=\"saleCar.").append("mortProfit")
//				.append("\" placeholder=\"").append("按揭费用").append("\"/>")
//				.append( "</Form-item>");
//				formContent.append("<Form-item label=\"").append("上牌费用")
//				.append("\" prop=\"").append("plateProfit").append("\">")
//				.append( "<i-input v-model=\"saleCar.").append("plateProfit")
//				.append("\" placeholder=\"").append("上牌费用").append("\"/>")
//				.append( "</Form-item>");
//				formContent.append("<Form-item label=\"").append("保险费用")
//				.append("\" prop=\"").append("insuranceProfit").append("\">")
//				.append( "<i-input v-model=\"saleCar.").append("insuranceProfit")
//				.append("\" placeholder=\"").append("保险费用").append("\"/>")
//				.append( "</Form-item>");
//			}
			List<BaseRoyaltyModel> royaltyList=royaltyDao.queryList(mapParam);
			if(CollectionUtils.isNotEmpty(royaltyList)){
				for (BaseRoyaltyModel baseRoyalty : royaltyList) {
					formContent.append("<Form-item label=\"").append(baseRoyalty.getName())
						.append("\" prop=\"").append(baseRoyalty.getMapName()).append("\">")
						.append( "<i-input v-model=\"productRoyalty.").append(baseRoyalty.getMapName())
						.append("\" placeholder=\"").append(baseRoyalty.getContent()).append("\"/>")
						.append( "</Form-item>");
				}
				formContent.append("<Form-item>");
				formContent.append("<i-button type=\"primary\" @click=\"handleRoyaltySubmit('formRoyaltyValidate')\">提交</i-button>");
				formContent.append("<i-button type=\"warning\" @click=\"reload\" style=\"margin-left: 8px\"/>返回</i-button>");
				formContent.append("</Form-item>");
			}
			result=formContent.toString();
		}
		return result;
	}
}
